﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-04-24 10:49:10
// 负责人：Sushee
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq.Expressions;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
namespace OF.Component.DAL
{
	
	/// <summary>
	/// 数据访问层
	/// </summary>	
	internal partial class MsgVerificationCode
	{		
    
        /// <summary>
    	/// 根据断言条件获取单个
    	/// </summary>
        public MsgVerificationCodeInfo QuerySingle(Expression<Func<MsgVerificationCodeInfo, bool>> predicate)
	    {
            return SQLServerProvider.CreateObject(ConnectionStringKey, predicate);
        }
        /// <summary>
    	/// 根据断言条件获获取列表
    	/// </summary>
	    public IList<MsgVerificationCodeInfo> QueryList(Expression<Func<MsgVerificationCodeInfo, bool>> predicate)
	    {
	        return SQLServerProvider.CreateList(ConnectionStringKey, predicate);
	    }

        /// <summary>
        ///  根据手机号与验证码类型取出记录
        /// </summary>
        /// <param name="typeId">类型</param>
        /// <param name="mobile">手机号</param>
        /// <param name="platformId">平台ID</param>
        /// <param name="checkCode">验证码</param>
        /// <returns></returns>
        public MsgVerificationCodeInfo GetByTypeAndMobile(int typeId, string mobile,int platformId,string checkCode)
        {
            Model.MsgVerificationCodeInfo info = null;

            string strSql = @"SELECT * FROM MsgVerificationCode WITH(NOLOCK) WHERE Mobile=@Mobile AND TypeId=@TypeId AND platformId=@platformId AND CheckCode=@CheckCode AND ExpirationDate>GETDATE()";

            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            db.AddInParameter(dbc, "@Mobile", DbType.String, mobile);
            db.AddInParameter(dbc, "@TypeId", DbType.Int32, typeId);
            db.AddInParameter(dbc, "@platformId", DbType.Int32, platformId);
            db.AddInParameter(dbc, "@CheckCode", DbType.String, checkCode);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
        }

        /// <summary>
        ///  根据手机号与验证码类型取出记录
        /// </summary>
        /// <param name="typeId">类型</param>
        /// <param name="mobile">验证码</param>
        /// <param name="platformId">平台ID</param>
        /// <returns></returns>
        public MsgVerificationCodeInfo GetByMobileAndTypeId(int typeId, string mobile, int platformId)
        {
            Model.MsgVerificationCodeInfo info = null;

            string strSql = @"SELECT * FROM MsgVerificationCode WITH(NOLOCK) WHERE Mobile=@Mobile AND TypeId=@TypeId AND platformId=@platformId AND ExpirationDate>GETDATE()";

            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            db.AddInParameter(dbc, "@Mobile", DbType.String, mobile);
            db.AddInParameter(dbc, "@TypeId", DbType.Int32, typeId);
            db.AddInParameter(dbc, "@platformId", DbType.Int32, platformId);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
        }

    }
	
}